<?php

namespace WinZ\DataMinerBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity(repositoryClass="WinZ\DataMinerBundle\Repository\SessionRepository")
 * @ORM\Table(name="sesions")
 */
class Session {
     /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Character", inversedBy="sessions")
     * @ORM\JoinColumn(onDelete="CASCADE", nullable=false)
     */
    protected $character;
    
    
    
     /**
     * @ORM\OneToMany(targetEntity="Node", mappedBy="session")
     */
    protected $nodes;
    
    
    /**
     * @ORM\Column(type="datetime", nullable=false)
     */
    protected $dtEnd;
    
    /**
     * @ORM\Column(type="datetime")
     */
    protected $dtBegin;

    /**
    * @ORM\Column(type="string", nullable=false)
    */
    protected $status = "";

    /**
    * @ORM\Column(type="string", nullable=false)
    */
    protected $goal = "";
    
    public function __construct()
    {
        $this->setDtBegin(new \DateTime());
        $this->setDtEnd(new \DateTime());
        $this->nodes = new ArrayCollection();
    }
    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set dtEnd
     *
     * @param \DateTime $dtEnd
     * @return Session
     */
    public function setDtEnd($dtEnd)
    {
        $this->dtEnd = $dtEnd;
    
        return $this;
    }

    /**
     * Get dtEnd
     *
     * @return \DateTime 
     */
    public function getDtEnd()
    {
        return $this->dtEnd;
    }

    /**
     * Set dtBegin
     *
     * @param \DateTime $dtBegin
     * @return Session
     */
    public function setDtBegin($dtBegin)
    {
        $this->dtBegin = $dtBegin;
    
        return $this;
    }

    /**
     * Get dtBegin
     *
     * @return \DateTime 
     */
    public function getDtBegin()
    {
        return $this->dtBegin;
    }

    /**
     * Set character
     *
     * @param \WinZ\DataMinerBundle\Entity\Character $character
     * @return Session
     */
    public function setCharacter(\WinZ\DataMinerBundle\Entity\Character $character)
    {
        $this->character = $character;
        $this->getCharacter()->setLastSession($this);
    
        return $this;
    }

    /**
     * Get character
     *
     * @return \WinZ\DataMinerBundle\Entity\Character 
     */
    public function getCharacter()
    {
        return $this->character;
    }
    
    
        /**
     * Add node
     *
     * @param \WinZ\DataMinerBundle\Entity\Node $nodes
     * @return Session
     */
    public function addNode(\WinZ\DataMinerBundle\Entity\Node $nodes)
    {
        $this->nodes[] = $nodes;
        return $this;
    }

    /**
     * Remove nodes
     *
     * @param \WinZ\DataMinerBundle\Entity\Node $nodes
     */
    public function removeNode(\WinZ\DataMinerBundle\Entity\Node $nodes)
    {
        $this->nodes->removeElement($nodes);
    }

    /**
     * Get nodes
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getNodes()
    {
        return $this->nodes;
    }
    
    
    /**
     * Get total amont of nodes
     *
     * @return integer
     */
    public function getTotalNodes()
    {
        $total = 0;
        foreach ($this->nodes AS $n) {
            $total += $n->getQty();
        }
        return $total;
    }
    
    /**
     * Get the session duration 
     *
     * @return integer
     */
    public function getDuration()
    {
        return $this->getDtEnd()->getTimestamp() - $this->getDtBegin()->getTimestamp();
    }

    
    
     /**
     * Get idle time
     *
     * @return /DateInterval
     */
    public function getTimeSinceLastHarvest()
    {
        $time = $this->getDtEnd()->getTimestamp()-$this->getDtBegin()->getTimestamp();
        
        foreach ($this->nodes AS $n) {
            $tmp = $this->getDtEnd()->getTimestamp() - $n->getDt()->getTimestamp();
            if($time > $tmp)
                $time = $tmp;
        }
        return $time;
    }    

    /**
     * Set status
     *
     * @param string $status
     * @return Session
     */
    public function setStatus($status)
    {
        $this->status = $status;
    
        return $this;
    }

    /**
     * Get status
     *
     * @return string 
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * Set goal
     *
     * @param string $goal
     * @return Session
     */
    public function setGoal($goal)
    {
        $this->goal = $goal;
    
        return $this;
    }

    /**
     * Get goal
     *
     * @return string 
     */
    public function getGoal()
    {
        return $this->goal;
    }
}